RGB -> Gray scale
Gray scale(灰階影像)
from PIL import Image
img2 = Image.open('GIF\\egg0.gif') #file path
img2 = img2.convert('L') # convert to L (Grayscale)
img2.save('GIF\\egg0_grayscale.gif') #file path
img2.close()
原圖(RGB)
灰階(Gray Scale)
gray scale -> black and white picture(黑白圖)
img2 = Image.open('GIF\\egg0_grayscale.gif')
width , heigth = img2.size #取得圖片的寬度 、高度
img2 = img2.convert('L')
for x in range(width):
for y in range(heigth):
if img2.getpixel((x,y)) >= 127:
img2.putpixel((x,y) , 255) #putpixel第一個參數是像素位置,第二個參數是要該像素變更的值,255(白色)
else:
img2.putpixel((x,y) , 0) #0(黑色)
img2.save('GIF\\egg0_blackAndWhite.gif')
img2.show()
img2.close()
黑白圖black and white picture
https://pillow.readthedocs.io/en/stable/reference/Image.html